package com.resou.reader.base;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;

public abstract class ToolbarActivity<P extends BasePresenter> extends AppCompatActivity {
    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(getColor(setStatusBarColor()));
        }
        // 这句很关键，注意是调用父类的方法
        super.setContentView(R.layout.activity_toolbar);
        initToolbar();
    }

    private int setStatusBarColor() {
        return R.color.white;
    }

    protected void initToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void setContentView(int layoutResID) {
        setContentView(View.inflate(this, layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
        LinearLayout rootLayout = findViewById(R.id.root_layout);
        rootLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    protected void setTitle(String msg) {
        TextView title = findViewById(R.id.tv_title);
        title.setText(msg);
    }

    protected void setRightBtn(String msg, View.OnClickListener onClickListener) {
        TextView rightBtn = findViewById(R.id.tv_right_btn);
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setText(msg);
        rightBtn.setOnClickListener(onClickListener);
    }

    protected void setRightImg(int img, View.OnClickListener onClickListener) {
        ImageView rightBtn = findViewById(R.id.tv_right_img);
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setBackgroundResource(img);
        rightBtn.setOnClickListener(onClickListener);
    }

}
